package com.dao;

import com.bean.Message;
import com.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hecn
 * @version 1.0
 * @date 2020/8/1 0001 19:27
 * 以及还书的操作中message表的变化两个基本实现很相似的功能。
 * @company
 */
public class MessageDao {
    /***
     * 查看用户自己的借阅情况
     * @param name
     * @return
     */
    public List<Message> getMessage(String name) {

        List<Message> list = new ArrayList<>();
        Message m = null;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select * from book.message where name=?";

        try {
            connection = JdbcUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, name);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                m = new Message();
                m.setName(resultSet.getString("name"));
                m.setBook_name(resultSet.getString("book_name"));
                m.setDate(resultSet.getString("date"));

                list.add(m);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(connection, preparedStatement, resultSet);
        }
        return list;
    }


    /***
     * 借书
     * @param message
     */
    public void addMessage(Message message) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        String sql = "insert into book.message(name, book_name, date) VALUES(?,?,?)";

        try {
            connection = JdbcUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, message.getName());
            preparedStatement.setString(2, message.getBook_name());
            preparedStatement.setString(3, message.getDate());
            preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(connection, preparedStatement);
        }
    }

    /***
     * 还书
     * @param name
     * @param book_name
     */
    public void returnMessage(String name, String book_name) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        String sql = "delete from book.message where name=? and book_name=?";

        try {
            connection = JdbcUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, name);
            preparedStatement.setString(2, book_name);
            preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(connection, preparedStatement);
        }
    }

    public int check(String name, String book_name){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        int flag=0;
        String sql = "select * from message where name=? and book_name=?";
        try {
            connection = JdbcUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,name);
            preparedStatement.setString(2,book_name);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                return flag=1;
            }
//            while(resultSet.next()){
//                String name1 = resultSet.getString("name");
//                String book_name1 = resultSet.getString("book_name");
//                if(name.equals(name1) && (book_name).equals(book_name1)){
//                    return flag = 1;
//                } }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.close(connection,preparedStatement,resultSet);
        }
        return flag;
    }


}
